package com.steveproject.parttimeworktree.utils;

import android.content.Context;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串检测工具
 */
public class StringUtils {

    /**
     * 判断是否为手机号
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(Context ctx, String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if (phone.length() != 11) {
            ToastUtil.showShort(ctx, "手机号应为11位数");
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if (!isMatch) {
                ToastUtil.showShort(ctx, "请填入正确的手机号");
            }
            return isMatch;
        }
    }

    public static String timesToString(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();
        date.setTime(Long.parseLong(time));
        return sdf.format(date);
    }

}
